package com.bytedance.ttnet.c;

import android.net.Uri;
import android.text.TextUtils;
import android.util.Pair;

import com.bytedance.common.utility.Logger;
import com.bytedance.frameworks.baselib.network.http.AppConfig;
import com.bytedance.retrofit2.client.Header;
import com.bytedance.retrofit2.client.Request;
import com.bytedance.retrofit2.client.SsResponse;
import com.bytedance.retrofit2.RequestBuilder;
import com.bytedance.retrofit2.ResponseWrapper;
import com.bytedance.ttnet.b.HttpRequestInfo;
import com.bytedance.ttnet.b.RequestParams;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import inline.UrlDemo;

public class SsOkHttpClient extends com.bytedance.frameworks.baselib.network.http.c.aa {
    public SsOkHttpClient() {

    }

    public void intercept(Request request, ResponseWrapper responseWrapper) throws Throwable {
        JSONObject v13;
        int v12;
        String v2_5;
        int v4_1;
        Uri v8;
        String v7;
        List v1_2;
        super.intercept(request, responseWrapper);
        if (request == null) {
            return;
        }

        if (responseWrapper == null) {
            return;
        }

        SsResponse v6 = responseWrapper.rawResponse;
        try {
            if (AppConfig.e() == null) {
//                goto label_27;
            } else {
                String v1_1 = AppConfig.e().a();
                if (android.support.design.a.isTextEmpty(v1_1)) {
//                    goto label_27;
                } else {
                    v1_2 = v6.getHeaders(v1_1);
                    if (v1_2 == null) {
//                      goto label_27;
                    } else if (v1_2.size() <= 0) {
//                      goto label_27;
                    } else {
                        ArrayList v2 = new ArrayList();
                        Iterator v3 = v1_2.iterator();
                        while (v3.hasNext()) {
                            Header bVar = (Header) v3.next();
                            ((List) v2).add(bVar.value);
                        }

                        AppConfig.e().a(v2);
                    }
                }
            }
        } catch (Throwable v1) {
            v1.printStackTrace();
        }

//        label_27:
        HttpRequestInfo v2_1 = null;
        Object v1_3 = v6.requestInfo;
        HttpRequestInfo v5_2;
        if ((v1_3 instanceof HttpRequestInfo)) {
            if (((HttpRequestInfo) v1_3).requestContext != null) {
                JSONObject v4 = new JSONObject();
                List v5 = v6.headers;
                int v3_1;
                for (v3_1 = 0; v3_1 < v5.size(); ++v3_1) {
                    try {
                        Object v2_3 = v5.get(v3_1);
                        if (v2_3 == null) {
//                            goto label_45;
                        } else if (TextUtils.isEmpty(((Header) v2_3).name)) {
//                            goto label_45;
                        } else {
                            v4.put(((Header) v2_3).name.toUpperCase(), ((Header) v2_3).value);
                        }
                    } catch (Exception v2_2) {
                        v2_2.printStackTrace();
                    }
                }

                if ((((HttpRequestInfo) v1_3).requestContext instanceof RequestParams)) {
                    ((RequestParams) ((HttpRequestInfo) v1_3).requestContext).headers = v4;
                }

                ((HttpRequestInfo) v1_3).requestContext.remoteIp = ((HttpRequestInfo) v1_3).timingRemoteIP;
            }
            v5_2 = (HttpRequestInfo) v1_3;
        } else {
            v5_2 = v2_1;
        }
        try {
            label_69:
            request.getHeader("X-SS-REQ-TICKET");
            v6.getHeader("X-SS-REQ-TICKET");
        } catch (Throwable v1) {
            v1.printStackTrace();
        }

        try {
            v7 = request.url;
            v8 = Uri.parse(v7);
            if (!v8.getHost().endsWith(android.support.design.a.z())) {
                return;
            }

            String[] v9 = new String[]{"Set-Cookie"};
            String[] v10 = new String[]{"sessionid", "tt_sessionid"};
            v4_1 = 0;
            while (true) {
                label_96:
                if (v4_1 > 0) {
                    return;
                }

                v1_2 = v6.getHeaders(v9[v4_1]);
                if (v1_2 == null) {
                    ++v4_1;
                    continue;
                }

                if (v1_2.size() <= 0) {
                    ++v4_1;
                    continue;
                }

                Iterator v11 = v1_2.iterator();
                label_103:

                while (v11.hasNext()) {
                    v1_3 = v11.next();
                    int v3_1 = 0;
                    while (true) {
                        if (v3_1 >= 2) {
                            break;
                        }

                        Matcher v2_4 = Pattern.compile(".*(((" + v10[v3_1] + "=[^;]*)|(" + v10[v3_1] + "=\"[\";]*))|(" + v10[v3_1] + "=.*$)).*").matcher(((Header) v1_3).value);
                        if (v2_4.matches()) {
                            v2_5 = v2_4.group(1);
                        } else {
                            v2_5 = null;
                        }
                        if (Logger.debug()) {
                            Logger.d("SsOkHttpClient", v2_5);
                        }

                        if (android.support.design.a.isTextEmpty(v2_5)) {
                            ++v3_1;
                            continue;
                        }

                        v12 = v6.status;
                        v13 = new JSONObject();
                        v2_5 = v8.getEncodedQuery();
                        if (android.support.design.a.isTextEmpty(v2_5)) {
//                goto label_146;
                        } else {
                            v13.put("url_query", v2_5);
                        }
                        if (!android.support.design.a.isTextEmpty(v5_2.timingRemoteIP)) {
                            v13.put("remote_ip", v5_2.timingRemoteIP);
                        }

                        if (v6.headers != null) {
                            v13.put("header_list", v6.headers.toString());
                        }

                        int v2_7 = v7.indexOf("?");
                        if (v2_7 == -1) {
                            v2_7 = v7.length();
                        }
                        com.bytedance.ttnet.utils.a.a("set_cookie", v7.substring(0, v2_7), v12, v13);
                        ++v3_1;
                        continue;
                    }
                }
                ++v4_1;
                continue;
            }
        } catch (Throwable v1) {
            v1.printStackTrace();
        }
    }

    public void intercept(RequestBuilder requestBuilder) {
        ArrayList v2;
        super.intercept(requestBuilder);
        if (requestBuilder != null) {
            String v3 = requestBuilder.url;
            List v0 = requestBuilder.headers;
            if (v0 == null) {
                v2 = new ArrayList();
            } else {
                v2 = (ArrayList) v0;
            }

            try {
                LinkedList v0_2 = new LinkedList();
                String v1 = UrlDemo.a(v3, ((List) v0_2));
                if (v1 != null) {
                    requestBuilder.url = v1;
                }

                if (!((List) v0_2).isEmpty()) {
                    Iterator v4 = ((List) v0_2).iterator();
                    while (v4.hasNext()) {
                        Pair<String, String> v0_3 = (Pair<String, String>) v4.next();
                        if (v0_3 == null) {
                            continue;
                        }

                        ((List) v2).add(new Header(v0_3.first, v0_3.second));
                    }
                }

                requestBuilder.headers = ((List) v2);
            } catch (Throwable v0_1) {
                v0_1.printStackTrace();
            }

            try {
                String v0_4 = com.bytedance.ttnet.utils.a.a(v3);
                if (android.support.design.a.isTextEmpty(v0_4)) {
                    return;
                }

                ((List) v2).add(new Header("X-SS-REQ-TICKET", v0_4));
                requestBuilder.headers = ((List) v2);
            } catch (Throwable v0_1) {
                v0_1.printStackTrace();
            }
        }
    }
}

